package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.settings.dao.UserDao;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.utils.SqlSessionUtil;
import com.bjpowernode.crm.vo.PaginationVO;
import com.bjpowernode.crm.workbench.dao.CustomerDao;
import com.bjpowernode.crm.workbench.dao.CustomerRemarkDao;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Customer;
import com.bjpowernode.crm.workbench.service.CustomerService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: crm
 * @description:
 * @author: 玉树临风的寒
 * @create: 2020-12-24 19:02:27
 **/
public class CustomerServiceImpl implements CustomerService {

    private CustomerDao customerDao = SqlSessionUtil.getSqlSession().getMapper(CustomerDao.class);
    private CustomerRemarkDao customerRemarkDao =  SqlSessionUtil.getSqlSession().getMapper(CustomerRemarkDao.class);
   private UserDao userDao = SqlSessionUtil.getSqlSession().getMapper(UserDao.class);



    public List<String> getCustomerName(String name) {
        List<String> sList= customerDao.getCustomerName(name);
        return sList;
    }

    public boolean save(Customer c) {
        boolean flag = true;
        int count = customerDao.save(c);
        if(count!=1){
            flag=false;
        }
        return flag;
    }

    public PaginationVO<Customer> pageList(Map<String, Object> map) {

        int total = customerDao.getTotalByCondition(map);

        List<Customer> dataList = customerDao.getCustomerListByCondition(map);
        PaginationVO<Customer> vo = new PaginationVO<Customer>();
        vo.setTotal(total);
        vo.setDataList(dataList);

        return vo;
    }

    public boolean delete(String[] ids) {
        boolean flag = true;
        //查询出需要删除的备注的数量
        int count1 =customerRemarkDao.getCountByAids(ids);

        //删除备注，返回受到影响的条数（实际删除的数量）
        int count2 = customerRemarkDao.deleteByAids(ids);
        if(count1!=count2){

            flag = false;

        }
        int count3 = customerDao.delete(ids);
        if(count3!=ids.length){

            flag = false;

        }


        return flag;
    }

    public Customer detail(String id) {

        Customer c = customerDao.detail(id);
        return c;
    }

    public Map<String, Object> getUserListAndActivity(String id) {
        List<User> uList =userDao.getUserList();

        Customer c = customerDao.getById(id);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("uList", uList);
        map.put("c", c);

        return map;
    }

    public boolean update(Customer c) {
        boolean flag = true;

        int count = customerDao.update(c);
        if(count!=1){

            flag = false;

        }
        //System.out.println("*******************************************************"+flag);

        return flag;
    }
}
